package com.maning.mndialoglibrary;


import com.maning.mndialoglibrary.base.BaseDialog;
import com.maning.mndialoglibrary.config.MDialogConfig;

import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;

/**
 * Created by maning on 2017/8/10.
 * 提示Dialog
 */
public class MStatusDialog {

    private Context mContext;
    private BaseDialog mDialog;

    private MDialogConfig mDialogConfig;

    private DependentLayout dialog_window_background;
    private DependentLayout dialog_view_bg;
    private Image imageStatus;
    private Text tvShow;

    public MStatusDialog(Context context) {
        this(context, new MDialogConfig.Builder().build());
    }

    public MStatusDialog(Context context, MDialogConfig dialogConfig) {
        mContext = context;
        mDialogConfig = dialogConfig;
        //初始化
        initDialog();
    }

    private void initDialog() {
        checkDialogConfig();
        try {
            LayoutScatter inflater = LayoutScatter.getInstance(mContext);
            Component mProgressDialogView = inflater.parse(ResourceTable.Layout_mn_status_dialog_layout, null,false);
            mDialog = new BaseDialog(mContext);
            mDialog.setContentCustomComponent(mProgressDialogView);
            mDialog.setTransparent(true);
            //获取布局
            dialog_window_background = (DependentLayout) mProgressDialogView.findComponentById(ResourceTable.Id_dialog_window_background);
            dialog_view_bg = (DependentLayout) mProgressDialogView.findComponentById(ResourceTable.Id_dialog_view_bg);
            imageStatus = (Image) mProgressDialogView.findComponentById(ResourceTable.Id_imageStatus);
            tvShow = (Text) mProgressDialogView.findComponentById(ResourceTable.Id_tvShow);

            //默认配置
            configView();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void checkDialogConfig() {
        if (mDialogConfig == null) {
            mDialogConfig = new MDialogConfig.Builder().build();
        }
    }

    private void configView() {
        //window背景
        //dialog_window_background.setBackgroundColor(mDialogConfig.backgroundWindowColor);

        //文字设置
        tvShow.setTextColor(mDialogConfig.textColor);
        tvShow.setTextSize((int)mDialogConfig.textSize);

        //弹框背景

        dialog_view_bg.setPadding(mDialogConfig.paddingLeft,mDialogConfig.paddingTop,mDialogConfig.paddingRight,mDialogConfig.paddingBottom
        );


        //图片宽高
        if (mDialogConfig.imgWidth > 0 && mDialogConfig.imgHeight > 0) {
            ComponentContainer.LayoutConfig layoutParams = imageStatus.getLayoutConfig();
            layoutParams.width = mDialogConfig.imgWidth;
            layoutParams.height = mDialogConfig.imgHeight;
            imageStatus.setLayoutConfig(layoutParams);
        }
    }

    public void show(String msg, PixelMapElement drawable) {
        show(msg, drawable, 2000);
    }

    public void show(String msg, PixelMapElement drawable, int delayMillis) {
        try {
            if (mDialog == null) {
                return;
            }
            if (mDialog.isShowing()) {
                mDialog.hide();
            }
            imageStatus.setImageElement(drawable);
            mDialog.setAutoClosable(true);
            mDialog.setDuration(delayMillis);
            tvShow.setText(msg);
            mDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismiss() {
        try {
            mContext = null;
            if (mDialog != null) {
                mDialog.hide();
                mDialog = null;
            }
            if (mDialogConfig != null && mDialogConfig.onDialogDismissListener != null) {
                mDialogConfig.onDialogDismissListener.onDismiss();
            }
        } catch (Exception e) {

        }
    }
}
